﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    public float moveSpeed = 50;
    //谁发射的我
    private TankBase fatherObj;
    public GameObject effObj;

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        this.transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    }

    private void OnTriggerEnter(Collider other)
    {
        //子弹打到Cube会爆炸
        //同样子弹打到不同阵营的对应也应该爆炸
        if ((other.CompareTag("Cube") || 
            (other.CompareTag("Player")) && fatherObj.CompareTag("Monster"))||
            (other.CompareTag("Monster")) && fatherObj.CompareTag("Player"))
        {
            //判断是否受伤
            //得到碰撞到的对象身上 是否有坦克相关的脚本 用里氏替换原则
            TankBase tankObj = other.GetComponent<TankBase>();
            if (tankObj)
                tankObj.Wound(fatherObj);

            //创建一个爆炸特效
            if (effObj)
            {
                GameObject eff = Instantiate(effObj, this.transform.position, this.transform.rotation);
                //由于该特效身上挂了音效,需要控制音效和音效音量
                AudioSource audioSource = eff.GetComponent<AudioSource>();
                if (audioSource)
                {
                    //根据音乐数据 设置音效大小和是否播放
                    //设置音效大小
                    audioSource.volume = GameDataMgr.Instance.musicData.audioVolume;
                    //音效是否播放
                    audioSource.mute = !GameDataMgr.Instance.musicData.audio;
                    //避免没有勾选 Play on Awake
                    audioSource.Play();
                }
            }
            Destroy(this.gameObject);
        }
        
    }

    public void SetFather(TankBase obj)
    {
        fatherObj = obj;
    }
}
